<?php
namespace Ucmaim\Controller\Admin;

class FinanceController extends CommonController{

    public function _initialize(){
        parent::_initialize();
    }

    protected $maparr=array();//csv

    // 可结算订单列表
    public function SettleAccount(){
        $Order = D('Order');
        $get = I('get.', '', 'trim');
        $condition = [];
        if($get['sn'] != ''){
            $condition['sn'] = $get['sn'];
        }

        if($get['buyer_name'] != ''){
            $condition['buyer'] = $get['buyer_name'];
        }

        if($get['seller_name'] != ''){
            $condition['seller'] = $get['seller_name'];
        }
        $orders = $Order->settlementOrders($condition);
        if( $orders['order_id'] > 0 ){
            $tmp = $orders;
            $orders = [];
            $orders[] = $tmp;
        }

        $list = gotoPage($orders, $this->pageSize);
        $orders = $list['data'];
        $page   = $list['page'];

        // 获取卖家支付宝账号
        foreach($orders as $k => $v){
            $seller_alipay = $Order->getSellerAlipay($v['order_sn']);
            $orders[$k]['seller_alipay'] = $seller_alipay;
        }

        //手续费设置
        $poundage = getConfig('POUNDAGE');
        $this->assign('poundage', $poundage);
        $this->assign('list', $orders);
        $this->assign('page', $page);
        $this->display();
    }

    // 到处到Excel
    // 到处到Excel
    public function toExcel(){
        $Order = D('Order');
        $orders = $Order->settlementOrders();
        if( !empty($orders) ){
            vendor('PHPExcel/PHPExcel');
            vendor('PHPExcel/PHPExcel/Writer');

            // 获取卖家支付宝账号
            foreach($orders as $k => $v){
                $seller_alipay = $Order->getSellerAlipay($v['order_sn']);
                $orders[$k]['seller_alipay'] = $seller_alipay;
            }

            //手续费设置
            $poundage = getConfig('POUNDAGE');
            $PHPExcel = new \PHPExcel();
            // 设置表头
            $PHPExcel->getActiveSheet()->setCellValue('A1', '交易流水号');
            $PHPExcel->getActiveSheet()->setCellValue('B1', '买家');
            $PHPExcel->getActiveSheet()->setCellValue('C1', '卖家');
            $PHPExcel->getActiveSheet()->setCellValue('D1', '商品名称');
            $PHPExcel->getActiveSheet()->setCellValue('E1', '订单金额');
            $PHPExcel->getActiveSheet()->setCellValue('F1', '手续费率(%)');
            $PHPExcel->getActiveSheet()->setCellValue('G1', '应结账金额');
            $PHPExcel->getActiveSheet()->setCellValue('H1', '卖家支付宝账号');
            $PHPExcel->getActiveSheet()->setCellValue('I1', '订单状态');

            foreach($orders as $k => $v){
                $n = $k + 2;
                $PHPExcel->getActiveSheet()->setCellValueExplicit('A' . $n, $v['order_sn'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $PHPExcel->getActiveSheet()->setCellValue('B' . $n, $v['buyer_name'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $PHPExcel->getActiveSheet()->setCellValue('C' . $n, $v['seller_name'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $PHPExcel->getActiveSheet()->setCellValue('D' . $n, $v['goods_title'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $PHPExcel->getActiveSheet()->setCellValue('E' . $n, order_amount($v['order_number'], $v['order_price']), \PHPExcel_Cell_DataType::TYPE_STRING);
                $PHPExcel->getActiveSheet()->setCellValue('F' . $n, $poundage, \PHPExcel_Cell_DataType::TYPE_STRING);
                $PHPExcel->getActiveSheet()->setCellValue('G' . $n, order_balance($v['order_number'], $v['order_price']), \PHPExcel_Cell_DataType::TYPE_STRING);
                $PHPExcel->getActiveSheet()->setCellValue('H' . $n, $v['seller_alipay'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $PHPExcel->getActiveSheet()->setCellValue('I' . $n, order_status($v['order_real_status']), \PHPExcel_Cell_DataType::TYPE_STRING);
            }

            $outputFileName = iconv('utf-8', 'gbk','应结帐订单列表_' . date('Y_m_d_H_i_s', time()) . '.xls');
            $xlsWriter = new \PHPExcel_Writer_Excel5($PHPExcel);
            header("Content-Type: application/force-download");
            header("Content-Type: application/octet-stream");
            header("Content-Type: application/download");
            header('Content-Disposition:inline;filename="'.$outputFileName.'"');
            header("Content-Transfer-Encoding: binary");
            header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
            header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header("Pragma: no-cache");
            $finalFileName = CACHE_PATH.'/'.MODULE_NAME.'/'.$outputFileName;
            $xlsWriter->save($finalFileName);
            echo file_get_contents($finalFileName);
            unlink($finalFileName);
        }
    }

    // 结算订单处理
    public function ProcessOrder(){
        $Order = D('Order');
        if( IS_POST ){
            $post =  I('post.');
            $hash = $post['hash'];
            compare_form_hash($hash);
            if( $post['deal'] === '1' ){
                $order_sn = $post['sn'];
                $order_info = $Order->settlementOrders(array('sn' => $order_sn));
                if( empty($order_info) )  $this->error('订单不存在');
                $Order->_balance($order_info) ? $this->success('操作成功', U('Admin/Finance/SettleAccount')) : $this->error('操作失败，请稍候再试', U('Admin/Finance/SettleAccount'));;
                exit();
            }else{
                $this->error('未发生操作', U('Admin/Finance/SettleAccount'));
            }
        }
        $order_sn = I('get.sn');
        $condition = array('sn' => $order_sn);
        $order_info = $Order->settlementOrders($condition);
        if( empty($order_info) )  $this->error('订单不存在');
        //添加卖家支付宝账号
        $goods_info = D('Goods')->getGoodsInfo($order_info['goods_id'], false);
        //获取卖家联系方式
        $buyer_info = D('Member')->memberInfo($order_info['seller_id']);
        $this->assign('order', $order_info);
        $this->assign('goods', $goods_info);
        $this->assign('buyer', $buyer_info);
        $this->assign('hash', form_hash());
        $this->display();
    }

    // 快捷操作 - 打款完成
    public function balance(){
        $order_sn = I('get.sn');
        $Order = D('Order');
        $condition['sn'] = $order_sn;
        $order_info = $Order->settlementOrders($condition);
        if( empty($order_info) )  $this->error('订单不存在');
        $Order->_balance($order_info) ? $this->success('操作成功', U('Admin/Finance/SettleAccount')) : $this->error('操作失败，请稍候再试', U('Admin/Finance/SettleAccount'));
    }

    //资金日志
    public function TradingRecords(){
        if( IS_POST ){
            $post = I('post.', '', 'trim');
            $kw   = $post['keyword'];
            $date = $post['date'];
            $map  = [];

            if(!empty($kw)){
                $map['A.order_sn|B.member_name|B.member_mobile'] = array('eq',$kw);
                $this->assign('keyword', $kw);
            }
            if($date){
                $tmp = explode('~', $date);
                $start_date = trim($tmp[0]);
                $end_date   = trim($tmp[1]);
                $start = strtotime($start_date);
                $end   = strtotime($end_date) + 86400;  //end需要加一天，防止当天的数据搜索不出来
                $map['A.log_time'] = array(array('EGT' , $start),array('ELT' , $end));
                $this->assign('startDate', $start_date);
                $this->assign('endDate',   $end_date);
            }
            session('search', $map);
        }elseif(!I('get.Page')){
            $map = [];
            session('search', null);
        }else{
            $map = session('search');
        }
        $Log = D('MoneyLog');
        $Log->alias('A');
        $Log->field('A.*, B.member_name AS master_name, B.member_mobile AS master_mobile, C.member_name AS slave_name, C.member_mobile AS slave_mobile');
        $Log->join('uc_member AS B ON A.log_master_id = B.member_id');
        $Log->join('uc_member AS C ON A.log_slave_id = C.member_id');
        $Log->order('A.log_time desc');
        $data = $Log->where($map)->select();

        $list = gotoPage($data, $this->pageSize);
        $this->assign('list', $list['data']);
        $this->assign('page', $list['page']);
        $this->display();
    }



}